using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using NovodexWrapper;

namespace marbletrack
{/// <summary>
    /// Right curved track piece
    /// </summary>
    public class RightCurvePiece : TrackPiece
    {
        public RightCurvePiece()
        {
            // Setup variables to default values
            SetupVariables(Vector3.Zero, Vector3.Zero);
        }

        public RightCurvePiece(Vector3 rotation, Vector3 modelPosition)
        {
            // Setup Variables with specified values
            SetupVariables(rotation, modelPosition);
        }

        // Setup Variables
        protected override void SetupVariables(Vector3 rotation, Vector3 modelPosition)
        {
            // Setup orientation variables
            modelRotation_ = rotation;
            CorrectPosition_ = Matrix.CreateTranslation(-Game1.SQUARESIZE, 0, -Game1.HALFSQUARESIZE)
            * Matrix.CreateRotationY(MathHelper.ToRadians(0));

            // Load model
            model_ = Game1.CurveRightModel;

            // Create and Load physics actor
            trackActor_ = physics.CreateTriangleMeshActor(model_.MeshVertexBuffers[0], model_.MeshIndexBuffers[0], transformMatrix_, Physics.TRACKPIECE_PHYSICS_GROUP, true, model_.FlipNormalsFlag);
            
            model_.FlagTextured = true;
        }

        public override void SetEntryExitPoints()
        {
            exitPoint_ = ModelPosition;
            if (modelRotation_ == directionFromWest)
            {
                exitPoint_.X += Game1.SQUARESIZE * 1.5f * Game1.SCALE;
                exitPoint_.Z += Game1.SQUARESIZE * 1.5f * Game1.SCALE;
                exitDirection_ = directionFromNorth;
            }
            else if (modelRotation_ == directionFromNorth)
            {
                exitPoint_.X -= Game1.SQUARESIZE * 1.5f * Game1.SCALE;
                exitPoint_.Z += Game1.SQUARESIZE * 1.5f * Game1.SCALE;
                exitDirection_ = directionFromEast;
            }
            else if (modelRotation_ == directionFromEast)
            {
                exitPoint_.X -= Game1.SQUARESIZE * 1.5f * Game1.SCALE;
                exitPoint_.Z -= Game1.SQUARESIZE * 1.5f * Game1.SCALE;
                exitDirection_ = directionFromSouth;
            }
            else if (modelRotation_ == directionFromSouth)
            {
                exitPoint_.X += Game1.SQUARESIZE * 1.5f * Game1.SCALE;
                exitPoint_.Z -= Game1.SQUARESIZE * 1.5f * Game1.SCALE;
                exitDirection_ = directionFromWest;
            }
        }
        #region Old Saver Method (Saver 1)
        /*
        public static void FromStringList(List<string> savedStuff)
        {
            Game1.modelList.Add(new RightCurvePiece(
                new Vector3((float)Convert.ToDouble(savedStuff[1]),
                            (float)Convert.ToDouble(savedStuff[2]),
                            (float)Convert.ToDouble(savedStuff[3])),
                (float)Convert.ToDecimal(savedStuff[4])));
            ((RightCurvePiece)Game1.modelList[Game1.modelList.Count - 1]).nextModelPosition_.X = (float)Convert.ToDouble(savedStuff[5]);
            ((RightCurvePiece)Game1.modelList[Game1.modelList.Count - 1]).nextModelPosition_.Y = (float)Convert.ToDouble(savedStuff[6]);
            ((RightCurvePiece)Game1.modelList[Game1.modelList.Count - 1]).nextModelPosition_.Z = (float)Convert.ToDouble(savedStuff[7]);
        }*/
        #endregion
    }
}
